package cn.kz.hrm.utils;

import cn.kz.hrm.domain.Pay;
import cn.kz.hrm.domain.PayAlipayInfo;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;


/**
 * 获取支付页面的一个form表单，
 */
public class AlipayUtils {

    public static String sendPayRequest(PayAlipayInfo alipayInfo, Pay pay) {
        try {
            /*
            //获得初始化的AlipayClient，回调时的内网地址。对数据进行加密
            参数：AlipayConfig.GATEWAY_URL 内网的穿透地址
            参数：alipayInfo.getAppid() 支付宝的支付Appid
            参数：AlipayConfig.CHARSET  设置编码集
            参数: alipayInfo.getAlipayPublicKey() 设置公钥。商户的公钥
            参数：AlipayConfig.SIGN_TYPE 设置他的加密方式。
             */
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.GATEWAY_URL, alipayInfo.getAppid(),
                    alipayInfo.getMerchantPrivateKey(),
                    "json", AlipayConfig.CHARSET, alipayInfo.getAlipayPublicKey(), AlipayConfig.SIGN_TYPE);

            //设置请求参数
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            alipayRequest.setReturnUrl(AlipayConfig.RETURN_URL); //success.html，回调返回的页面
            alipayRequest.setNotifyUrl(AlipayConfig.NOTIFY_URL); //异步回调的地址，同时会访问一个controller。是支付的页面

            //商户订单号，商户网站订单系统中唯一订单号，必填，唯一的订单号
            String out_trade_no = new String(pay.getOrdernum());
            //付款金额，必填
            Float money = pay.getPrice();
//            //yhptest的注释 从分转换为圆
//            money = money.divide(new BigDecimal(100));
//            System.out.println(money.toString());
            String total_amount = new String(money.toString());
            //订单名称，必填
            String subject = new String("线上课程的服务订单！");
            //商品描述，可空
            String body = new String("该课程的价格为："+pay.getPrice()+"圆，请准时进行学习！");

            alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                    + "\"total_amount\":\"" + total_amount + "\","
                    + "\"subject\":\"" + subject + "\","
                    + "\"body\":\"" + body + "\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
            //请求
            System.out.println("查看跳转页面的参数：" + alipayRequest);
            String result = alipayClient.pageExecute(alipayRequest).getBody();
            System.out.println("购买课程支付跳转页面:"+result);
            return result;

            /*
            <form name="punchout_form" method="post" action="https://openapi.alipaydev.com/gateway.do?charset=utf-8&method=alipay.trade.page.pay&sign=Z6IsbPoDXLC7gAK54n89fQo8I%2FmjaqS%2B%2FEkcLmhuGsoIeHkXj8X9H8C2aVLGj2pxnuQA1%2BUbrx%2By1Wv03HcXl%2F%2BpgbY1ROuhe%2B6iywWWnVK3tjndFNc14%2FBFmwN709lvN0OFLBPMnkSYekc7qzXXY0NSmxlg%2FxhpE%2FM48LDfdTojR9klhd3LAGZKv2efMSUEzyZ2L0ONJPhn1KSmcjYMafw9G4yEFIS4SvBzG4%2B4YORnq0HuUmJma9EIAQneJZDifu2seLdUaxZtgsxfYbovZIg5KpBwlCiHv%2F5lFuqfloVqzwQvM4J6SNrguMWzVVLX%2FTBd9y3it8C1U1Fw%2B3CbNQ%3D%3D&return_url=http%3A%2F%2F127.0.0.1%2Fsuccess.html&notify_url=http%3A%2F%2Fqjx8af.natappfree.cc%2FalipayNotify&version=1.0&app_id=2016082100302609&sign_type=RSA2&timestamp=2020-06-14+21%3A24%3A55&alipay_sdk=alipay-sdk-java-3.7.4.ALL&format=json">
                <input type="hidden" name="biz_content" value="{&quot;out_trade_no&quot;:&quot;200614092465182&quot;,&quot;total_amount&quot;:&quot;0.90&quot;,&quot;subject&quot;:&quot;宠物乐园订单！&quot;,&quot;body&quot;:&quot;[摘要]：生成基础版洗澡订单&quot;,&quot;product_code&quot;:&quot;FAST_INSTANT_TRADE_PAY&quot;}">
                <input type="submit" value="立即支付" style="display:none" >
            </form>
            <script>document.forms[0].submit();</script>
             */
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
